<?php

class AdminController extends MController
{
	public function init()
	{
		$this->foot_js[] = 'js/admin.js';
	}
	
	public function actionIndex($p = 1, $checkAdminName = false)
	{
		$this->foot_js[] = 'js/plugins/forms/jquery.ibutton.js';
		if ($checkAdminName)
		{
			$this->checkAdminName();
		}
		
		$page_size = 10;
		//搜索条件
		$condition = $this->getCondition();
		$res = $this->model('AdminModel')->pager($condition, $page_size);
		$this->var['pages'] = $res['pages'];
		$this->var['res'] = $res['res'];
		$this->var['status'] = array('已禁用', '已启用');
		$role_list = array();
		foreach ($this->model('RoleModel')->findAll() as $role)
		{
			$role_list[$role['role_id']] = $role;
		}
		$this->var['role_list'] = $role_list;
		
		$this->renderData('index');
	}
	
	public function actionAdd()
	{
		if (Yii::app()->request->isAjaxRequest) {
			//保存权限
			$this->save();
		}
		$this->var['role_list'] = $this->model('RoleModel')->findAll();
		$this->renderData('add');
	}
	
	public function actionEditPass($admin_id = 0)
	{
		$admin_id = (int) $admin_id;
		if ($admin_id < 1)
		{
			$this->util->show_404();
		}
		if (Yii::app()->request->isPostRequest) {
			$this->save_pass($admin_id);
		}
		$this->var['admin'] = $this->model('AdminModel')->findByPk($admin_id);
		$this->renderPartial('edit_pass', $this->var);
	}
	
	/**
	 * 此方法的权限写在AdminModel中，不需要在rights.php中添加
	 */
	public function actionMyPass()
	{
		if (Yii::app()->request->isPostRequest) {
			$this->save_pass($admin_id);
		}
		$admin = array(
			'admin_id' => Yii::app()->user->admin_id,
			'admin_name' => Yii::app()->user->admin_name
		);
		$this->var['admin'] = $admin;
		$this->var['no_forward'] = 1;
		$this->renderPartial('edit_pass', $this->var);
	}
	
	public function actionEditRole($admin_id = 0)
	{
		$admin_id = (int) $admin_id;
		if ($admin_id < 1)
		{
			$this->util->show_404();
		}
		if (Yii::app()->request->isPostRequest) {
			$this->save_role($admin_id);
		}
		$this->var['admin'] = $this->model('AdminModel')->findByPk($admin_id);
		$this->var['role_list'] = $this->model('RoleModel')->findAll();
		$this->renderPartial('edit_role', $this->var);
	}
	
	public function actionEditStatus($admin_id = 0)
	{
		$admin_id = (int) $admin_id;
		$status = (int) Yii::app()->request->getPost('status');
		if ($admin_id < 1)
		{
			$this->util->show_404();
		}
		if (Yii::app()->request->isAjaxRequest) {
			$AdminModel = new AdminModel();
			if ($AdminModel->save_status($admin_id, $status))
			{
				echo 'true';die;
			}
			else
			{
				echo 'false';die;
			}
		}
	}
	
	public function actionDel($admin_id = 0)
	{
		$admin_id = (int) $admin_id;
		if ($admin_id > 0)
		{
			$this->model('AdminModel')->deleteByPk($admin_id);
			$this->redirect(BASE_URL . 'admin/index');
		}
		$this->util->show_404();
	}
	
	/**
	 * 获取搜索条件
	 */
	private function getCondition()
	{
		$condition = array();
		$keywords = trim(Yii::app()->request->getQuery('keywords'));
		$role_id = Yii::app()->request->getQuery('role_id');
		$this->var['keywords'] = $keywords;
		$this->var['role_id'] = $role_id;
		$is_search = '';
		
		$condition['condition'] = '1=1';
		if (!empty($keywords))
		{
			$condition['condition'] .= ' AND admin_name LIKE :admin_name';
			$condition['params'][':admin_name'] = '%' . $keywords . '%';
			$is_search = 1;
		}
		if (!empty($role_id))
		{
			$condition['condition'] .= ' AND role_id=:role_id';
			$condition['params'][':role_id'] = $role_id;
			$is_search = 1;
		}
		$this->var['is_search'] = $is_search;
		return $condition;
	}
	
	/**
	 * 保存管理员
	 * 私有方法只能通过曝光的动作来调用
	 * $admin_id大于0时修改管理员
	 */
	private function save() 
	{
		$admin_name = trim(Yii::app()->request->getPost('admin_name'));
		$admin_pass = trim(Yii::app()->request->getPost('admin_pass'));
		$confirm_pass = trim(Yii::app()->request->getPost('confirm_pass'));
		$role_id = (int) Yii::app()->request->getPost('role_id');
		if (empty($admin_name))
		{
			$this->util->return_json('用户名不能为空', false);
		}
		if (empty($admin_pass))
		{
			$this->util->return_json('密码不能为空', false);
		}
		if ($admin_pass != $confirm_pass)
		{
			$this->util->return_json('确认密码不正确', false);
		}
		if ($role_id < 1)
		{
			$this->util->return_json('请选择权限组', false);
		}
		
		$AdminModel = new AdminModel();
		if ($AdminModel->admin_save($admin_name, $admin_pass, $role_id))
		{
			$this->util->return_json('保存成功', true, BASE_URL . 'admin/index');
		}
		else
		{
			$this->util->return_json('保存失败', false);
		}
	}
	
	private function save_pass($admin_id)
	{
		$admin_pass = trim(Yii::app()->request->getPost('admin_pass'));
		$confirm_pass = trim(Yii::app()->request->getPost('confirm_pass'));
		if (empty($admin_pass))
		{
			$this->util->return_json('密码不能为空', false);
		}
		if ($admin_pass != $confirm_pass)
		{
			$this->util->return_json('确认密码不正确', false);
		}
		$AdminModel = new AdminModel();
		if ($AdminModel->save_pass($admin_id, $admin_pass))
		{
			$this->util->return_json('保存成功', true, BASE_URL . 'admin/index');
		}
		else
		{
			$this->util->return_json('保存失败', false);
		}
	}
	
	private function save_role($admin_id)
	{
		$role_id = (int) Yii::app()->request->getPost('role_id');
		if ($role_id < 1)
		{
			$this->util->return_json('请选择权限组', false);
		}
		$AdminModel = new AdminModel();
		if ($AdminModel->save_role($admin_id, $role_id))
		{
			$this->util->return_json('保存成功', true, BASE_URL . 'admin/index');
		}
		else
		{
			$this->util->return_json('保存失败', false);
		}
	}
	
	/**
	 * 验证用户名是否存在
	 * @param string $admin_name
	 */
	private function checkAdminName()
	{
		$admin_name = Yii::app()->request->getPost('fieldValue');
		if ($this->model('AdminModel')->findByAttributes(array('admin_name' => $admin_name)))
		{
			$ret = array('admin_name', false);
		}
		else
		{
			$ret = array('admin_name', true);
		}
		echo json_encode($ret);die;
	}
}